import os
import numpy as np
import cv2 as cv

from generateLabel import Letter, Province
Root_path= 'D:\\project\\python\\datasets\\ccpd_green\\images/'
Save_path = Root_path+'changeNameTrain/'
fila_names=os.listdir(Save_path)

def drawRect(img,p1):
    cv.line(img,(int(p1[0]),int(p1[2])),(int(p1[1]),int(p1[2])),(0, 0, 255),thickness=1,lineType=cv.LINE_AA)
    cv.line(img,(int(p1[1]),int(p1[2])),(int(p1[1]),int(p1[3])),(0, 0, 255),thickness=1,lineType=cv.LINE_AA)
    cv.line(img,(int(p1[1]),int(p1[3])),(int(p1[0]),int(p1[3])),(0, 0, 255),thickness=1,lineType=cv.LINE_AA)
    cv.line(img,(int(p1[0]),int(p1[3])),(int(p1[0]),int(p1[2])),(0, 0, 255),thickness=1,lineType=cv.LINE_AA)

def getPonit(names:str)->list:
    fist_spilt=names.split('-')
    point_str=fist_spilt[3]
    second_spilt=point_str.split('_')
    left_up_str,left_down_str,right_up_str,right_down_str=second_spilt[2],second_spilt[1],second_spilt[3],second_spilt[0]
    left_up_x,left_up_y=int(left_up_str.split('&')[0]),int(left_up_str.split('&')[1])
    left_down_x,left_down_y=int(left_down_str.split('&')[0]),int(left_down_str.split('&')[1])
    right_up_x,right_up_y,=int(right_up_str.split('&')[0]),int(right_up_str.split('&')[1])
    right_down_x, right_down_y=int(right_down_str.split('&')[0]),int(right_down_str.split('&')[1])
    return [[left_up_x,left_up_y],[left_down_x,left_down_y],[right_up_x,right_up_y],[right_down_x, right_down_y]]

def getCarNumbers(names:str)->list:
    fist_spilt=names.split('-')
    car_number_str=fist_spilt[-3]
    car_number_list=car_number_str.split('_')
    car_number_index=0
    for car_number in car_number_list:
        if car_number_index==0:
            car_number_list[car_number_index]=Province[int(car_number)]
        else:
            car_number_list[car_number_index]=Letter[int(car_number)]
        car_number_index=car_number_index+1
    return car_number_list
    
def perspective():
    for file in fila_names:
        img = cv.imread(Save_path+file, 1)
        rows, cols, channels = img.shape
        origin_point= getPonit(file)
        carnumbers=getCarNumbers(file)
        p1 = np.float32(origin_point)  # 左上角，左下角，右上角，右下角
        p2 = np.float32([[0,0], [0,140], [440,0], [440,140]])
        transform_m = cv.getPerspectiveTransform(p1,p2)
        dst = cv.warpPerspective(img, transform_m, (440, 140))
        spacings=int(440/9*1.1)
        start_plate=spacings
        upTobottom=140
        carnumber_index=0
        for letter in carnumbers:
            if carnumber_index==0:
               rect=(spacings*0.1,start_plate,0+upTobottom*0.15,140-upTobottom*0.13)
               drawRect(dst,rect)
               start_plate=start_plate+spacings*0.04
            elif carnumber_index==1:
               rect=(start_plate,start_plate+spacings,0+upTobottom*0.15,140-upTobottom*0.13)
               drawRect(dst,rect)
               start_plate=start_plate+spacings+spacings*0.7
               spacings=spacings*0.7
            # else:
            #    rect=(start_plate,start_plate+spacings,0+upTobottom*0.15,140-upTobottom*0.13)
            #    drawRect(dst,rect)
            #    start_plate=start_plate+spacings+spacings
            # # inverse matrix
            # M=np.linalg.inv(transform_m)
            # （j, i）
            # x = (M[0][0] * 0 + M[0][1] * 0 + M[0][2]) / (M[2][0] * 0 + M[2][1] * 0 + M[2][2])
            # y = (M[1][0] * 0 + M[1][1] * 0 + M[1][2]) / (M[2][0] * 0 + M[2][1] * 0 + M[2][2])
            # print(x,y)
            #x = (M[0][0] * j + M[0][1] * i + M[0][2]) / (M[2][0] * j + M[2][1] * i + M[2][2])
            #y = (M[1][0] * j + M[1][1] * i + M[1][2]) / (M[2][0] * j + M[2][1] * i + M[2][2])
            carnumber_index=carnumber_index+1
        # left_up,right_up,left_up,right_down
        # first_letter=(start_plate,start_plate+start_plate,upTobottom*0.15,140-upTobottom*0.13)
        # start_plate=start_plate+spacings
        # drawRect(dst,first_letter)
        # spacings=spacings*0.7
        # start_plate=start_plate+spacings
        # second_letter=(start_plate,start_plate+spacings,upTobottom*0.15,140-upTobottom*0.13)
        # drawRect(dst,second_letter)
        cv.imshow('change', dst)
        cv.imshow('original', img)
        cv.waitKey(0)
        cv.destroyAllWindows()
perspective()